﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ScoreMS.Model;

namespace ScoreMS.BLL
{
    /// <summary>
    /// 试卷 事务逻辑
    /// </summary>
    public class Examination_paparBS
    {
        /// <summary>
        /// 获取单个试卷
        /// </summary>
        /// <param name="course_id"></param>
        /// <returns></returns>
        static public Examination_papar GetExamination_papar(int ex_id)
        {
            using (scoremsdbEntities db = new scoremsdbEntities())
            {
                Examination_papar model = new Examination_papar();
                var papar = db.examination_papar.FirstOrDefault(x => x.ex_id == ex_id);
                model.ex_id = papar.ex_id;
                model.smallquestion_count = papar.smallquestion_count;
                model.bigquestion_count = papar.bigquestion_count;
                model.course_id = papar.course_id;
                return model;
            }
        }

        /// <summary>
        /// 获取试卷题数
        /// </summary>
        /// <param name="course_id"></param>
        /// <returns></returns>
        static public int GetProblemCount(int ex_id)
        {
            using (scoremsdbEntities db = new scoremsdbEntities())
            {
                var papar = db.examination_papar.FirstOrDefault(x => x.ex_id == ex_id);
                return papar.smallquestion_count;
            }
        }

        /// <summary>
        /// 试卷已录入题目的数量
        /// </summary>
        /// <param name="course_id"></param>
        /// <returns></returns>
        static public int GetPreparedProblemCount(int ex_id)
        {
            using (scoremsdbEntities db = new scoremsdbEntities())
            {
                var papar = db.examination_papar.FirstOrDefault(x => x.ex_id == ex_id);
                return papar.problem.Count;
            }
        }

        /// <summary>
        /// 返回试卷属于的课程
        /// </summary>
        /// <param name="course_id"></param>
        /// <returns></returns>
        static public string GetCourseID(int ex_id)
        {
            using (scoremsdbEntities db = new scoremsdbEntities())
            {
                Examination_papar model = new Examination_papar();
                var papar = db.examination_papar.FirstOrDefault(x => x.ex_id == ex_id);
                return papar.course_id;
            }
        }

        /// <summary>
        /// 根据课程ID，获取试卷列表
        /// </summary>
        /// <param name="course_id"></param>
        /// <returns></returns>
        static public List<Examination_papar> GetExamination_paparList(string course_id)
        {
            using (scoremsdbEntities db = new scoremsdbEntities())
            {
                var ret = db.examination_papar.Where(x => x.course_id == course_id);
                List<Examination_papar> list = new List<Examination_papar>();
                foreach (var papar in ret)
                {
                    Examination_papar model = new Examination_papar();
                    model.ex_id = papar.ex_id;
                    model.smallquestion_count = papar.smallquestion_count;
                    model.bigquestion_count = papar.bigquestion_count;
                    model.course_id = papar.course_id;
                    list.Add(model);
                }
                return list;
            }
        }

        /// <summary>
        /// 添加试卷
        /// </summary>
        /// <param name="course"></param>
        /// <returns>成功返回true</returns>
        static public bool AddExamination_papar(Examination_papar model)
        {
            try
            {
                using (scoremsdbEntities db = new scoremsdbEntities())
                {
                    db.examination_papar.AddObject(new examination_papar()
                    {
                        smallquestion_count = model.smallquestion_count,
                        bigquestion_count = model.bigquestion_count,
                        course_id = model.course_id
                    });
                    db.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 录入指定试卷的单个学生每题成绩（详细），若未录入总成绩，则一并录入总成绩
        /// </summary>
        /// <param name="ex_id">试卷ID</param>
        /// <param name="student_id">学生ID</param>
        /// <param name="points">分数List</param>
        /// <returns></returns>
        static public bool AddScore(int ex_id, string student_id, List<int> points)
        {
            try
            {
                using (scoremsdbEntities db = new scoremsdbEntities())
                {
                    string course_id = GetCourseID(ex_id);
                    var score = db.score.FirstOrDefault(x => x.course_id == course_id && x.student_id == student_id);
                    if (score == null)  //未录入总成绩
                    {
                        AddScore(ex_id, student_id, points.Sum());
                    }

                    var list = ProblemBS.GetProblemList(ex_id);
                    for (int i = 0; i < list.Count; i++)
                    {
                        var problem = list[i];
                        var score_detail = db.score_detail.FirstOrDefault(x => x.problem_id == problem.problem_id && x.student_id == student_id);
                        if (score_detail == null)   //未录入
                        {
                            Score_detailBS.AddScore_detail(new Score_detail()
                            {
                                student_id = student_id,
                                score_detail_points = points[i],
                                problem_id = problem.problem_id
                            });
                        }
                        else    //已录入，覆盖
                        {
                            score_detail.score_detail_points = points[i];
                            db.SaveChanges();
                        }
                    }
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 录入指定课程的单个学生考试成绩（试卷总成绩）
        /// </summary>
        /// <param name="ex_id">试卷ID</param>
        /// <param name="student_id">学生ID</param>
        /// <param name="points">分数List</param>
        /// <returns></returns>
        static public bool AddScore(int ex_id, string student_id, int points)   //TODO:修改成绩（将录入都改成接受覆盖的！）
        {
            try
            {
                using (scoremsdbEntities db = new scoremsdbEntities())
                {
                    string course_id = GetCourseID(ex_id);
                    bool ret = ScoreBS.AddScore(student_id, course_id, points);
                    if (ret)
                        return true;
                    else
                        return false;
                }
            }
            catch
            {
                return false;
            }
        }
    }
}
